<template>
  <v-app>
    <SideBar @selectPages="selectPages" @logout="logout"></SideBar>
    <v-main>
      <loginPage v-if="pageSelected == -1" @login="logininfo"></loginPage>
      <chooseClass v-if="pageSelected == 0"></chooseClass>
      <delClass v-if="pageSelected == 1"></delClass>
      <queryClass v-if="pageSelected == 2"></queryClass>
      <queryScore v-if="pageSelected == 3"></queryScore>
      <stuManage v-if="pageSelected == 10"></stuManage>
      <addScore v-if="pageSelected == 11"></addScore>
      <releaseSignin v-if="pageSelected == 12"></releaseSignin>
      <admin v-if="pageSelected == 20"></admin>
    </v-main>
    <v-dialog v-model="signining" max-width="600px">
      <v-card :title="signin_class">
        <v-card-actions>
          <v-btn text="签到" @click="sign_in"></v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-app>
</template>

<script>
import SideBar from "./components/SideBar.vue";
import chooseClass from "./components/student/chooseClass.vue";
import delClass from "./components/student/delClass.vue";
import queryClass from "./components/student/queryClass.vue";
import queryScore from "./components/student/queryScore.vue";
import addScore from "./components/teacher/addScore.vue";
import stuManage from "./components/teacher/stuManage.vue";
import admin from "./components/admin/admin.vue";
import loginPage from "./components/loginPage.vue";
import releaseSignin from "./components/teacher/releaseSignin.vue";
import axios from "axios";
axios.defaults.baseURL = "https://api.apiopen.top";

export default {
  name: "App",

  components: {
    SideBar,
    chooseClass,
    delClass,
    queryClass,
    queryScore,
    addScore,
    admin,
    loginPage,
    stuManage,
    releaseSignin,
  },
  async created() {
    this.userinfo =
      localStorage.getItem("userinfo") != null
        ? JSON.parse(localStorage.getItem("userinfo"))
        : {
            name: "请登录",
            id: 0,
            school: "",
            role: -1,
          };
    this.islogin = localStorage.getItem("userinfo") != null;
    this.gotoDefaultPage();
    let dept_info=[]
    await axios({
        method: "get",
        url: "http://127.0.0.1:8000/admin/dept",
        params: {
          dept_id: '',
          dept_name:''
        },
      }).then((res) => {
        console.log(res);
        dept_info=res.data.data
      });
      let dept_names=[]
      for(var i=0;i<dept_info.length;i++)
      {
        dept_names.push(dept_info[i].dept_name)
      }
      console.log(dept_names)
      this.dept_names=dept_names
  },
  data() {
    return {
      pageSelected: -1,
      userinfo: {},
      axios: axios,
      islogin: false,
      semester: "2023春季",
      signining: false,
      signin_class: "",
      signin_id: "",
      dept_names: [],
    };
  },
  methods: {
    selectPages(pageidx) {
      console.log(pageidx);
      this.pageSelected = pageidx;
      console.log(this.pageSelected);
    },
    async get_signin_info() {
      let student_id = this.userinfo.id;
      let signing = false;
      let couese_name = "";
      let signin_id = "";
      await axios({
        method: "get",
        url: "http://127.0.0.1:8000/student/signin_query",
        params: {
          student_id: student_id,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.results.length != 0) {
          signing = true;
          couese_name = res.data.results[0].course_name;
          signin_id = res.data.results[0].signin_id;
        }
      });
      this.signining = signing;
      this.signin_class = couese_name;
      this.signin_id = signin_id;
      console.log(this.signining);
    },
    async sign_in() {
        let signin_id=this.signin_id
        let student_id=this.userinfo.id
      await axios({
        method: "get",
        url: "http://127.0.0.1:8000/student/signin",
        params: {
          student_id: student_id,
          signin_id: signin_id,
        },
      }).then((res) => {
        console.log(res);
      });
      this.signining = false;
    },
    gotoDefaultPage() {
      if (this.userinfo.role == "student") {
        this.pageSelected = 0;
        this.get_signin_info();
      } else if (this.userinfo.role == "teacher") this.pageSelected = 10;
      else if (this.userinfo.role == "admin") this.pageSelected = 20;
    },
    async logininfo(info) {
      console.log(info);
      this.userinfo = info;
      localStorage.setItem("userinfo", JSON.stringify(info));
      this.gotoDefaultPage();
      this.islogin = true;
    },
    logout() {
      this.userinfo = {
        name: "请登录",
        id: 0,
        school: "",
        role: -1,
      };
      localStorage.removeItem("userinfo");
      this.islogin = false;
      this.pageSelected = -1;
    },
  },
};
</script>
<!-- 
<script>
    export default{
        data(){
            return {
                msg:'Hello组件'
            }
        },
        methods:{
            changeMsg(){
               return this.msg+='a'
            }
        },
    }
</script> -->
